<?php
include_once "../../../../wp-load.php";
$query = "SELECT * FROM wp_schedule where ev_blogid = ". get_current_blog_id();

if(isset($_GET["cateno"]) && $_GET["cateno"] != ""){
    $query .= " and ev_cateno = ".$_GET["cateno"];
}

$query .= " order by ev_startday";

$results = $wpdb->get_results($query);

$event_data_arr = array();
$i=0;

foreach($results as $row){

    $start_datetime = $row->ev_startday;
    $end_datetime = $row->ev_endday;
    // 시작일과 종료일의 날짜 시간이 같을 경우 종료일이 수정폼에 안나오는 문제로 인한 처리
    if($row->ev_startday == $row->ev_endday){        
        $end_datetime = date("Y-m-d H:i",  strtotime($start_datetime)).":01";
    }
    //주간반복처리    
    if($row->ev_repeat == 1){        
        $start_timestampe = strtotime($start_datetime);
        $end_timestampe = strtotime($end_datetime);
       
        while($start_timestampe > $_GET["start"]){

            $before_week_start = mktime(date("H", $start_timestampe), date("i", $start_timestampe),date("s", $start_timestampe), date("m", $start_timestampe), date("d", $start_timestampe)-7, date("Y", $start_timestampe));
            $before_week_end = mktime(date("H", $end_timestampe), date("i", $end_timestampe),date("s", $end_timestampe), date("m", $end_timestampe), date("d", $end_timestampe)-7, date("Y", $end_timestampe));

            $event_data_arr[$i]["id"] = $row->ev_no;
            $event_data_arr[$i]["title"] = $row->ev_title;
            $event_data_arr[$i]["start"] = date("Y-m-d H:i", $before_week_start).":00";
            $event_data_arr[$i]["end"] = date("Y-m-d H:i",  $before_week_end).":01";
            $event_data_arr[$i]["color"] = $row->ev_color;
            $event_data_arr[$i]["location"] = $row->ev_location;
            $event_data_arr[$i]["content"] = $row->ev_content;
            $event_data_arr[$i]["repeat"] = $row->ev_repeat;
            $event_data_arr[$i]["cateno"] = $row->ev_cateno;

            $start_timestampe = $before_week_start;
            $end_timestampe = $before_week_end;
            $i++;            
        }

        $start_timestampe = strtotime($start_datetime);
        $end_timestampe = strtotime($end_datetime);

        while($end_timestampe < $_GET["end"]){

            $after_week_start = mktime(date("H", $start_timestampe), date("i", $start_timestampe),date("s", $start_timestampe), date("m", $start_timestampe), date("d", $start_timestampe)+7, date("Y", $start_timestampe));
            $after_week_end = mktime(date("H", $end_timestampe), date("i", $end_timestampe),date("s", $end_timestampe), date("m", $end_timestampe), date("d", $end_timestampe)+7, date("Y", $end_timestampe));

            $event_data_arr[$i]["id"] = $row->ev_no;
            $event_data_arr[$i]["title"] = $row->ev_title;
            $event_data_arr[$i]["start"] = date("Y-m-d H:i", $after_week_start).":00";
            $event_data_arr[$i]["end"] = date("Y-m-d H:i",  $after_week_end).":01";
            $event_data_arr[$i]["color"] = $row->ev_color;
            $event_data_arr[$i]["location"] = $row->ev_location;
            $event_data_arr[$i]["content"] = $row->ev_content;
            $event_data_arr[$i]["repeat"] = $row->ev_repeat;
            $event_data_arr[$i]["cateno"] = $row->ev_cateno;

            $start_timestampe = $after_week_start;
            $end_timestampe = $after_week_end;
            $i++;
        }
         
    }

    //월간반복
    if($row->ev_repeat == 2){
        $start_timestampe = strtotime($start_datetime);
        $end_timestampe = strtotime($end_datetime);

        while($start_timestampe > $_GET["start"]){

            $before_month_start = mktime(date("H", $start_timestampe), date("i", $start_timestampe),date("s", $start_timestampe), date("m", $start_timestampe)-1, date("d", $start_timestampe), date("Y", $start_timestampe));
            $before_month_end = mktime(date("H", $end_timestampe), date("i", $end_timestampe),date("s", $end_timestampe), date("m", $end_timestampe)-1, date("d", $end_timestampe), date("Y", $end_timestampe));

            $event_data_arr[$i]["id"] = $row->ev_no;
            $event_data_arr[$i]["title"] = $row->ev_title;
            $event_data_arr[$i]["start"] = date("Y-m-d H:i", $before_month_start).":00";
            $event_data_arr[$i]["end"] = date("Y-m-d H:i",  $before_month_end).":01";
            $event_data_arr[$i]["color"] = $row->ev_color;
            $event_data_arr[$i]["location"] = $row->ev_location;
            $event_data_arr[$i]["content"] = $row->ev_content;
            $event_data_arr[$i]["repeat"] = $row->ev_repeat;
            $event_data_arr[$i]["cateno"] = $row->ev_cateno;

            $start_timestampe = $before_month_start;
            $end_timestampe = $before_month_end;
            $i++;
        }

        $start_timestampe = strtotime($start_datetime);
        $end_timestampe = strtotime($end_datetime);

        while($end_timestampe < $_GET["end"]){

            $after_month_start = mktime(date("H", $start_timestampe), date("i", $start_timestampe),date("s", $start_timestampe), date("m", $start_timestampe)+1, date("d", $start_timestampe), date("Y", $start_timestampe));
            $after_month_end = mktime(date("H", $end_timestampe), date("i", $end_timestampe),date("s", $end_timestampe), date("m", $end_timestampe)+1, date("d", $end_timestampe), date("Y", $end_timestampe));

            $event_data_arr[$i]["id"] = $row->ev_no;
            $event_data_arr[$i]["title"] = $row->ev_title;
            $event_data_arr[$i]["start"] = date("Y-m-d H:i", $after_month_start).":00";
            $event_data_arr[$i]["end"] = date("Y-m-d H:i",  $after_month_end).":01";
            $event_data_arr[$i]["color"] = $row->ev_color;
            $event_data_arr[$i]["location"] = $row->ev_location;
            $event_data_arr[$i]["content"] = $row->ev_content;
            $event_data_arr[$i]["repeat"] = $row->ev_repeat;
            $event_data_arr[$i]["cateno"] = $row->ev_cateno;

            $start_timestampe = $after_month_start;
            $end_timestampe = $after_month_end;
            $i++;
        }

    }

    $event_data_arr[$i]["id"] = $row->ev_no;
    $event_data_arr[$i]["title"] = $row->ev_title.$m;
    $event_data_arr[$i]["start"] = $start_datetime;
    $event_data_arr[$i]["end"] = $end_datetime;
    $event_data_arr[$i]["color"] = $row->ev_color;
    $event_data_arr[$i]["location"] = $row->ev_location;
    $event_data_arr[$i]["content"] = $row->ev_content;
    $event_data_arr[$i]["repeat"] = $row->ev_repeat;
    $event_data_arr[$i]["cateno"] = $row->ev_cateno;
    $i++;
}

header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: application/json");
echo json_encode($event_data_arr);

?>
